import { getPerformanceStatement } from '@/api/report'
import Pagination from '@/components/Pagination/index'
import { getSales } from '@/api/salesman'

export default {
  name: 'Salesman-Report',
  components: { Pagination },

  data(){
    return{
      list: null,
      statistics: null,
      listLoading: false,
      listQuery: {
        salesman: null,
        from_date: null,
        to_date: null
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      datetime: null,
      sales_options:[
        {
          value: null,
          label: '不限'
        }
      ],
      salesman:null
    }
  },

  created(){
    this.getSales()
  },

  methods:{
    getSales: async function() {
      const options = this.sales_options
      const res = await getSales();
      for(const v of res.list){
        options.push({
          value:v.id,
          label:v.nickname
        })
      }
    },
    getList() {

      if(!this.listQuery.salesman){
        this.$message.error('请选择业务员');
        return
      }

      if(!this.listQuery.from_date || !this.listQuery.to_date){
        this.$message.error('请选择时间');
        return
      }

      this.listLoading = true
      getPerformanceStatement(this.listQuery).then(response => {
        this.list = response.list
        this.total = 1
        this.listLoading = false
      }).catch(error => {
        this.list = []
        this.total = 0
        this.listLoading = false
        Promise.reject(error)
      })
    },

    handleSearch() {
      if (this.listQuery.salesman == '') {
        this.listQuery.salesman = null
      }
      if (!this.datetime) {
        this.listQuery.from_date = null
        this.listQuery.to_date = null
      } else {
        this.listQuery.from_date = this.datetime[0]
        this.listQuery.to_date = this.datetime[1]
      }
      this.list = []
      this.getList()
    }
  }
}
